#ifndef __M_ECHO_H__
#define __M_ECHO_H__

#include "server.hpp"
class EchoServer
{
private:
    Muduo::TcpServer _server;
private:
    void OnClosed(const Muduo::PtrConnection& conn)
    {
        LOGDEBUG("close connection:%p", conn.get());
    }
    void OnConnected(const Muduo::PtrConnection& conn)
    {
        LOGDEBUG("new connection: %p", conn.get());
    }
    void OnMessage(const Muduo::PtrConnection& conn, Muduo::Buffer* buf)
    {
        conn->Send(buf->ReadPosition(), buf->ReadAbleSize());
        buf->MoveReadOffest(buf->ReadAbleSize());
        conn->Shutdown();
    }
public:
    EchoServer(int port)
    :_server(port)
    {
        _server.SetThreadCount(1);
        // _server.EnableInactiveRelease(10);
        //设置用户提供的回调函数
        _server.SetCloseCallback(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
        _server.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
    }

    void Start()
    {
        //启动服务器
        _server.Start();
    }
};



#endif